﻿using System;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Controls;

namespace WPFWorkbookDesigner
{
    public static class SpreadHelper
    {
        [Obsolete]
        public static void Dowork(this IWorkbookSet set, Action todo)
        {
            set.GetLock();
            try
            {
                todo();
            }
            finally
            {
                set.ReleaseLock();
            }
        }

        public static void Dowork(this IWorkbookSet set, Action<IWorkbookSet> todo)
        {
            set.GetLock();
            try
            {
                todo(set);
            }
            finally
            {
                set.ReleaseLock();
            }
        }

        public static void Dowork(this WorkbookView view, Action<WorkbookView> todo)
        {
            view.GetLock();
            try
            {
                todo(view);
            }
            finally
            {
                view.ReleaseLock();
            }
        }

        [Obsolete]
        public static void Dowork(this WorkbookView view, Action todo)
        {
            view.GetLock();
            try
            {
                todo();
            }
            finally
            {
                view.ReleaseLock();
            }
        }

        [Obsolete]
        public static T Dowork<T>(this IWorkbookSet set, Func<T> toReturn)
        {
            set.GetLock();
            try
            {
                return toReturn();
            }
            finally
            {
                set.ReleaseLock();
            }
        }

        public static T Dowork<T>(this IWorkbookSet set, Func<IWorkbookSet, T> toReturn)
        {
            set.GetLock();
            try
            {
                return toReturn(set);
            }
            finally
            {
                set.ReleaseLock();
            }

        }
    }
}